﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeiXinO2O;
using System.IO;
using WeixinSb.Model;

namespace WeixinSB.Service.Controllers
{
    public class WeixinSBController : Controller
    {
        public ActionResult Index()
        {
            return Content("测试正常");
        }
        //
        // GET: /WeixinSB/
        AppModel app = new AppModel();

        public ActionResult Upload()
        {
            try
            {
                var file = Request.Files["pic"] as HttpPostedFileBase;
                if (file == null)
                {
                    app.Message = "file is null";
                }
                else
                {
                    var dic = "/AppUpload/" + DateTime.Now.ToString("yyyy-MM-dd");
                    if (!Directory.Exists(Server.MapPath(dic)))
                    {
                        Directory.CreateDirectory(Server.MapPath(dic));
                    }

                    var fileName = DL_helper.funcGetTicks().ToString() + ".jpg";

                    file.SaveAs(Server.MapPath(dic + "/" + fileName));

                    Upload_Image_bean bean = new Upload_Image_bean()
                    {
                        WxupId = DL_helper.funcGetTicks().ToString(),
                        WxupTitle = Request["title"],
                        WxupImagePath = dic + "/" + fileName,
                        WxupModels = Request["models"],
                        WxupDeviceName = Request["deviceName"],
                        WxupType = Request["type"],
                        WxCreateAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                    };

                    app.Ret = bean.Insert() > 0;
                }
            }
            catch (Exception ex)
            {
                app.Message = ex.ToString();
            }


            return Json(app, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult List()
        {
            try
            {
                string pageSize = Request["count"];
                var maxId = Convert.ToInt64(Request["max_id"]);
                var type = Request["type"];

                var dao = new Upload_Image_dao()
                {
                    propSelect = " top {0} *".J_Format(pageSize.J_ToInt32()),
                    propOrder = " order by WxupId desc",
                    propWhere = "WxupType = '{0}' and WxStatus = '{1}' and WxupId > '{2}'".J_FormatReplaceSqlQuery(type, "Y", maxId),
                };
                var list = dao.QueryList<Upload_Image_bean>();

                var res = new List<WeiXinUploadModel>();

                foreach (var item in list)
                {
                    res.Add(new WeiXinUploadModel
                    {
                        DisLikeCount = item.WxupDisLikeCount,
                        Id = item.WxupId,
                        ImagePath = CDNConfig.ImgPrefix + item.WxupImagePath,
                        LikeCount = item.WxupLikeCount,
                        Title = item.WxupTitle,
                        Models = GetModels(item.WxupModels,item.WxupDeviceName),
                        CreateAt = item.WxCreateAt,
                    });
                }

                app.DataModel = res;
                app.Ret = true;
            }
            catch (Exception ex)
            {
                app.Message = ex.ToString();
            }

            return Json(app, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Like()
        {
            try
            {
                string like = Request["like"];
                var id = Request["id"];

                var bean = Upload_Image_bean.funcGetByID(id);
                if (like == "Y")
                {
                    bean.WxupLikeCount++;
                    bean.Update();
                }
                else
                {
                    bean.WxupDisLikeCount++;
                    bean.Update();
                }

                app.Ret = true;
            }
            catch (Exception ex)
            {
                app.Message = ex.ToString();
            }

            return Json(app, JsonRequestBehavior.AllowGet);
        }

        string GetDate(string s)
        {
            var dt = s.J_ToDateTime();
            var now = DateTime.Now;
            if (dt.Year == now.Year && dt.Month == now.Month && dt.Day == now.Day)
            {
                return dt.ToString("HH:mm");
            }

            return dt.ToString("yyyy-MM-dd");
        }

        string GetModels(string m, string deviceName)
        {
            try
            {
                return PhoneNameResolver.Resolve(m, deviceName).CanonicalModel;
            }
            catch (Exception)
            {

            }
            return m;
        }
    }
}
